package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.MatchShareBean;
import cn.bocweb.spinning.model.Bean.SecondTimeJudgeBean;
import cn.bocweb.spinning.model.Bean.TestDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.util.WXUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import de.greenrobot.event.EventBus;

public class TestDetailActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.exam_detail_level)
    TextView examDetailLevel;
    @Bind(R.id.exam_detail_icon)
    ImageView examDetailIcon;
    @Bind(R.id.exam_detail_exit)
    ImageView examDetailExit;
    @Bind(R.id.exam_detail_nickname)
    TextView examDetailNickname;
    @Bind(R.id.exam_detail_point)
    TextView examDetailPoint;
    @Bind(R.id.exam_detail_time_spend)
    TextView examDetailTimeSpend;
    @Bind(R.id.exam_detail_point_level)
    TextView examDetailPointLevel;
    @Bind(R.id.exam_detail_money)
    TextView examDetailMoney;
    @Bind(R.id.exam_detail_money_level)
    TextView examDetailMoneyLevel;
    @Bind(R.id.exam_detail_answer)
    TextView examDetailAnswer;
    @Bind(R.id.exam_detail_wrong)
    TextView examDetailWrong;
    @Bind(R.id.exam_detail_rank)
    TextView examDetailRank;

    int friend_1_timeline_2 = 0;
    boolean secondTest;
    String testId;
    String libName;
    String libid;
    UserPresenter presenter;
    boolean collectDeleted;
    boolean isActivity;
    boolean activityFinished;
    @Bind(R.id.test_result_share_friend)
    LinearLayout testResultShareFriend;
    @Bind(R.id.test_result_share_timeline)
    LinearLayout testResultShareTimeline;
    @Bind(R.id.textview_score)
    TextView textviewScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_detail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        initIntent();
        initClickBtn();
        dialog.show();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private void initClickBtn() {
        examDetailExit.setOnClickListener(this);
        examDetailAnswer.setOnClickListener(this);
        examDetailWrong.setOnClickListener(this);
        examDetailRank.setOnClickListener(this);
        testResultShareFriend.setOnClickListener(this);
        testResultShareTimeline.setOnClickListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIntent() {
        presenter = new UserPresenterImp();

        Intent intent = getIntent();
        testId = intent.getStringExtra(Constant.TEST_DETAIL);
        libName = intent.getStringExtra(Constant.SCHOOL_TO_TEST);
        libid = intent.getStringExtra(Constant.SCHOOL_TO_TEST_DETAIL_LIB);
        isActivity = intent.getBooleanExtra(Constant.TO_TEST_DETAIL_IS_ACITVITY, false);
        secondTest = intent.getBooleanExtra(Constant.TO_TEST_DETAIL_IS_SECOND, false);
        if (secondTest) {
            presenter.secondTimeJudge();
        }
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);

        LibListBean.DataEntity dataEntity1 = new LibListBean.DataEntity();
        dataEntity1.readLibListIntoLocal(this);
        if (TextUtils.isEmpty(libName)) {
            textviewScore.setText(dataEntity1.getTitle() + "分数:");
            collectDeleted = false;
        } else {
            textviewScore.setText(libName + "分数:");
            collectDeleted = true;
        }

        presenter.testDetail(dataEntity.getUserid(), testId);
    }

    public void onEventMainThread(SecondTimeJudgeBean bean) {
        long nowTime = System.currentTimeMillis();
        long endTime = Long.parseLong(bean.getEnd()) * 1000;
        activityFinished = nowTime > endTime;
    }

    public void onEventMainThread(TestDetailBean bean) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (bean.getReturn_code() == 0) {
            if (bean.getData().getScore().equals("100")) {
                examDetailWrong.setVisibility(View.INVISIBLE);
            }

            String icon = bean.getData().getUthumb();
            if (icon == null || icon.isEmpty()) {
                examDetailIcon.setImageResource(R.mipmap.drawer_icon_unlogin);
            } else {
                Glide.with(this).load(icon).transform(new GlideCircle(this)).into(examDetailIcon);
            }
            String nickname = bean.getData().getNickname();
            if (nickname == null || nickname.isEmpty()) {
                examDetailNickname.setText(Constant.NICKNAME_UNLOGIN);
            } else {
                examDetailNickname.setText(nickname);
            }
            examDetailLevel.setText(bean.getData().getTitle());
            examDetailMoney.setText(bean.getData().getScope());
            examDetailMoneyLevel.setText(bean.getData().getScopeintro());
            examDetailPoint.setText(bean.getData().getScore());
            examDetailPointLevel.setText(bean.getData().getSubtitle());
            int time = Integer.parseInt(bean.getData().getSpend_time());
            String timeSpend = (time / 60) + "分" + (time % 60) + "秒";
            examDetailTimeSpend.setText(timeSpend);
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }

    }

    public void onEventMainThread(ErrorBean bean) {
        hideLoading();
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }

    public void onEventMainThread(MatchShareBean bean) {
        if (bean.getReturn_code() == 0) {
            if (friend_1_timeline_2 == 1) {
                WXUtil.WXShareToFriend(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
            } else {
                WXUtil.WXShareToTimeline(bean.getUrl(), this, "纺织大学堂", "我参加了梳理知识大奖赛，快来看看我考了第几名吧!");
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.exam_detail_exit: {
                finish();
                break;
            }
            case R.id.exam_detail_answer: {
                if (secondTest) {
                    if (!activityFinished) {
                        DeviceUtil.MyToast(this, "考试还未结束，暂不开放");
                        break;
                    }
                }
                intent = new Intent(this, CheckAnswerActivity.class);
                intent.putExtra(Constant.CHECK_ANSWER_LIBID, libid);
                intent.putExtra(Constant.CHECK_COLLECT_DELETE, collectDeleted);
                intent.putExtra(Constant.CHECK_ANSWER_TESTID, testId);
                startActivity(intent);
                break;
            }
            case R.id.exam_detail_wrong: {
                if (secondTest) {
                    if (!activityFinished) {
                        DeviceUtil.MyToast(this, "考试还未结束，暂不开放");
                        break;
                    }
                }
                intent = new Intent(this, CheckAnswerActivity.class);
                intent.putExtra(Constant.CHECK_ANSWER_LIBID, libid);
                intent.putExtra(Constant.CHECK_COLLECT_DELETE, collectDeleted);
                intent.putExtra(Constant.CHECK_ERROR_TESTID, testId);
                startActivity(intent);
                break;
            }
            case R.id.exam_detail_rank: {
                intent = new Intent(this, RankActivity.class);
                intent.putExtra(Constant.TO_RANK_LIBID, libid);
                intent.putExtra(Constant.TO_RANK_TITLE, libName);
                intent.putExtra(Constant.TO_RANK_IS_ACTIVITY, isActivity);
                startActivity(intent);
                break;
            }
            case R.id.test_result_share_friend: {
                friend_1_timeline_2 = 1;
                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                dataEntity.readInfoFromLocal(this);
                presenter.matchShare(dataEntity.getUserid(), testId);
                break;
            }
            case R.id.test_result_share_timeline: {
                friend_1_timeline_2 = 2;
                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                dataEntity.readInfoFromLocal(this);
                presenter.matchShare(dataEntity.getUserid(), testId);
                break;
            }
        }
    }
}
